package com.example.check.web.controller;

import com.example.common.GetPassWord;
import com.example.common.ThisSystemException;
import com.example.check.entity.PermissionEntity;
import com.example.check.entity.RoleEntity;
import com.example.check.entity.UserEntity;
import com.example.check.service.PermissionServiceImpl;
import com.example.check.service.RoleService;
import com.example.check.service.UserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

/**
 * Created by 13296 on 2017/7/12.
 */
@Controller
public class LoginController {
    @Autowired
    UserService fun;
    @Autowired
    RoleService rfun;
    @Autowired
    PermissionServiceImpl pfun;

    private final Log log = LogFactory.getLog(getClass());

    @RequestMapping("/login.do")
    public String login(String account, String password,HttpServletRequest request){
        try {
            Subject subject= SecurityUtils.getSubject();
            UserEntity u = fun.login(account,password,subject);


            HttpSession session = request.getSession();
            session.setAttribute("currentUser",u);

            log.info("登陆成功！" +'\n');

            ArrayList<UserEntity> userList = fun.searchAllUser();
            session.setAttribute("userList",userList);
            ArrayList<RoleEntity> roleList = rfun.searchAllRole();
            session.setAttribute("roleList",roleList);
            ArrayList<PermissionEntity> permissionList = pfun.searchAllPermission();
            session.setAttribute("permissionList",permissionList);
        }catch (Exception e){
            request.setAttribute("message",e.getMessage());
            return "forward:/login.jsp";
        }

        return "redirect:/index.do";
    }
    @RequestMapping("/index.do")
    public String index(){
        return "index";
    }

    @RequestMapping("/welcome.do")
    public String welcome(HttpServletRequest request){
        request.setAttribute("message", "");
        return "index";
    }
    /**
     *退出登录
     * @return
     */
    @RequestMapping("/logout.do")
    public String logout(HttpSession session){
        //让session失效
        session.invalidate();
        return "redirect:/login.jsp";
    }

    @RequestMapping(method=RequestMethod.GET,path="/updatepassword.do")
    public String updatepasswordView(){
        return null;
    }

    @RequestMapping(method=RequestMethod.POST,path="/updatepassword.do")
    public String updatepassword(String oldPassword,String newPassword,String newPasswordConfirm,HttpServletRequest request)throws Exception{
        HttpSession session=request.getSession();
        UserEntity u=(UserEntity)session.getAttribute("currentUser");

        try{
            fun.updatePassword(u.getId(),oldPassword, newPassword, newPasswordConfirm);
        }catch(ThisSystemException e){
            request.setAttribute("message", e.getMessage());
            return "updatepassword";
        }
        request.setAttribute("message", "修改密码成功!");
        return "firstPage";
    }

    @RequestMapping(method=RequestMethod.GET,path="/enroll.do")
    public String enrollView(){
        return null;
    }

    @RequestMapping(method=RequestMethod.POST,path="/enroll.do")
    public String enroll(String account, String password, String passwordAgain, String name, String sex, String telephone, String email, String address,HttpServletRequest request)throws Exception{
        HttpSession session=request.getSession();
        try{
            if(!password.equals(passwordAgain)){
                throw new Exception("两次输入密码有误，请重新填写!");
            }
            String roleLevel = "2";//一般用户
            if(sex.equals("1")){
                sex="男";
            }
            else{
                sex="女";
            }
            fun.enroll(account,password,name,roleLevel,sex,telephone,email,address);
        }catch(Exception e){
            request.setAttribute("message", e.getMessage());
            return "enroll";
        }
        request.setAttribute("message", "注册成功，请开始登录");
        return "redirect:/login.jsp";
    }

    @RequestMapping(method = RequestMethod.GET, path="/firstPage.do")
    public String firstPage(HttpServletRequest request) throws Exception{

        HttpSession session = request.getSession();

        ArrayList<UserEntity> userList = fun.searchAllUser();
        session.setAttribute("userList",userList);
        ArrayList<RoleEntity> roleList = rfun.searchAllRole();
        session.setAttribute("roleList",roleList);
        ArrayList<PermissionEntity> permissionList = pfun.searchAllPermission();
        session.setAttribute("permissionList",permissionList);
        return null;
    }

    @RequestMapping(method = RequestMethod.GET, path = "/wrongPage.do")
    public String wrongPage(){
        return "wrongPage";
    }
}
